# This script replicates the hate crime and term frequency figures seen in our paper and appendix

# Load prerequisites
rm(list = ls())

library(dplyr)
library(data.table)
library(readxl)
library(tidyr)
library(ggplot2)
library(extrafont)
library(showtext)

# Add font
font_location <- "/usr/share/fonts/truetype/ebgaramond/EBGaramond12-Regular.ttf"

font_import(pattern = "Garamond", prompt = FALSE)
font_add(family = "EB Garamond 12", regular = font_location)

setwd("~/Dropbox/Diaspora_Narratives/Submission/SecStud/Replication")

## Hate crimes

df <- fread("02_data/hate_crimes_complaints_sum.csv")

ggplot(data = df[df$Group != "Asian", ], aes(x = Year, y = Complaints, linetype = Group)) +
  geom_line(alpha = 0.3) +
  geom_line(data = df[df$Group == "Asian", ], aes(x = Year, y = Complaints, linetype = Group), linewidth = 0.8) +
  labs(linetype = "Targeted Group") +
  theme_bw() +
  theme(text = element_text(family = "EB Garamond 12", size = 12))

ggsave("03_output/Graphs/hate_crime_complaints.pdf", width = 10, height = 6)
ggsave("03_output/Graphs/hate_crime_complaints.png", width = 10, height = 6)

## Term frequency

df <- fread("02_data/term_data.csv")

df |>
  ggplot(aes(x = Date, y = 100 * Proportion, color = Government_lab)) +
  geom_point() +
  geom_line() +
  scale_color_grey() +
  facet_grid(OA + Dictionary ~ ., scales = "free_y") +
  labs(color = "Affiliation", y = "Percent of total tokens") +
  theme_bw()+
  theme(text = element_text(family = "EB Garamond 12", size = 12)) +
  guides(alpha = "none")


ggsave("03_output/Graphs/term_freq_time.pdf", width = 7, height = 5)
ggsave("03_output/Graphs/term_freq_time.png", width = 7, height = 5)

